package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.common.exception.BusinessException;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.mapper.IntegralGradeMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 * 积分等级表 服务实现类
 * </p>
 *
 * @author lsyandwh
 * @since 2022-12-02
 */
@Service
@Transactional
public class IntegralGradeServiceImpl extends ServiceImpl<IntegralGradeMapper, IntegralGrade> implements IntegralGradeService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public BigDecimal getIntegralByUserId(String usrId) {

        //1.根据用户id查询用户信息，获取总积分
        UserInfo userInfo = userInfoMapper.selectById(Long.parseLong(usrId));
        Integer integral = userInfo.getIntegral();

        //2.根据积分判断用户借款额度
        QueryWrapper<IntegralGrade> wrapper = new QueryWrapper<>();
        wrapper.le("integral_start",integral)//小于区间
                .gt("integral_end",integral);//大于区间

        IntegralGrade integralGrade = baseMapper.selectOne(wrapper);

        if(null == integralGrade){
            throw new BusinessException(ResponseEnum.USER_AMOUNT_LESS_ERROR);
        }

        return integralGrade.getBorrowAmount();
    }
}
